import numpy as np
import matplotlib.pylab as plt
from twisted.python.util import println


def step_function(x):
    if x > 0:
        return 1
    else:
        return 0

def step_function_vec(x):
    y = x > 0
    return y.astype(np.int8)

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

def relu(x):
    return np.maximum(0, x)

def plot_step_function():
    x = np.arange(-5, 5, 0.1)
    y = step_function_vec(x)
    plt.plot(x, y)
    plt.ylim(-0.1, 1.1)  # y轴的范围
    plt.show()

def plot_sigmoid():
    y2 = sigmoid(x)
    plt.plot(x, y2)
    plt.ylim(-0.1, 1.1)  # y轴的范围
    plt.show()

def identity_function(x):
    return x

def softmax(a):
    c=np.max(a)
    exp_a = np.exp(a - c)
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a
    return y

def plot_softmax():
    x = np.arange(-1000, 1000, 1)
    println(x)
    y = softmax(x)
    println(y)
    plt.plot(x, y)
    plt.ylim(-0.1, 1.1)  # y轴的范围
    plt.show()

def init_network():
    network = {}
    network['W1'] = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]])
    network['b1'] = np.array([0.1, 0.2, 0.3])
    network['W2'] = np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]])
    network['b2'] = np.array([0.1, 0.2])
    network['W3'] = np.array([[0.1, 0.3], [0.2, 0.4]])
    network['b3'] = np.array([0.1, 0.2])
    return network

def forward(network, x):
    W1, W2, W3 = network['W1'], network['W2'], network['W3']
    b1, b2, b3 = network['b1'], network['b2'], network['b3']
    a1 = np.dot(x, W1) + b1  # 第一层的输出
    z1 = sigmoid(a1)  # 第一层通过激活函数的输出
    a2 = np.dot(z1, W2) + b2  # 第二层的输出
    z2 = sigmoid(a2)  # 第二层通过激活函数的输出
    a3 = np.dot(z2, W3) + b3  # 第三层的输出
    y = identity_function(a3)  # 第三层通过激活函数的输出
    return y

if __name__ == '__main__':
    print(step_function(0.2))

    print(step_function_vec(np.array([-1.0, 1.0, 2.0])))

    # plot_step_function()
    # plot_sigmoid()

    networkx = init_network()
    x = np.array([1.0, 0.5])
    y = forward(networkx, x)
    println(y)


    # println(softmax(np.array([0.3, 2.9, 4.0])))
    plot_softmax()